package com.ocelot.san.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.commons.lang.StringUtils;

import com.ocelot.san.dto.User;
import com.ocelot.san.form.UserForm;
import com.ocelot.san.service.RoleService;
import com.ocelot.san.service.UserService;


@ManagedBean(name = "userController")
@ViewScoped
public class UserController {

	private UserForm userForm;
	
	@ManagedProperty(value = "#{userService}")
	private UserService userService;
	
	@ManagedProperty(value = "#{roleService}")
	private RoleService roleService;
	
	@PostConstruct
	protected void init() {
		userForm = new UserForm();
		userForm.setEditUser(new User());
		userForm.setRoles(roleService.fetchAll());
		refreshUserList();
	}
	
	public void submitUser(ActionEvent event) {
		if (validateForm()) {
			userService.save(userForm.getEditUser());
			// clear Data
			userForm.setEditUser(new User());
			refreshUserList();
		}
	}
	

	protected void refreshUserList() {
		userForm.setUsers(userService.fetchAll());
	}
	
	public boolean validateForm() {
		List<String> errors = new ArrayList<String>();
		
		if(StringUtils.isEmpty(userForm.getEditUser().getLogin())) {
			errors.add("login es requerido");
		}
		if(StringUtils.isEmpty(userForm.getEditUser().getName())) {
			errors.add("Nombre es requerido");
		}
		if(StringUtils.isEmpty(userForm.getEditUser().getName())) {
			errors.add("Contraseña es requerida");
		}
		if(StringUtils.isEmpty(userForm.getEditUser().getPasswordConfirm())) {
			errors.add("Confirmar contraseña es requerida");
		}
		if(!userForm.getEditUser().getPasswordConfirm().equals(userForm.getEditUser().getPassword())) {
			errors.add("Contraseña y Confirmar contraseña deben ser idénticas");
		}
		if(errors.size() > 0) {
			for(String error:errors){
				 FacesMessage message=new FacesMessage(error);  
				 message.setSeverity(FacesMessage.SEVERITY_INFO);  
				 FacesContext ctx= FacesContext.getCurrentInstance();  
				 ctx.addMessage("userForm", message);
			}
			return false;
		}
		return true;
	}

	
	public String editItem() {
		String idToEdit = userForm.getToEdit();
		User toModify = getUserById(idToEdit);
		toModify.setPasswordConfirm(toModify.getPassword());
		userForm.setEditUser(toModify);
		return null;
	}

	public String removeItem() {
		String indexToRemove = userForm.getToRemove();
		User removeItem = getUserById(indexToRemove);
		if (removeItem != null) {
			userService.delete(removeItem.getLogin());
			userForm.getUsers().remove(removeItem);
			userForm.setEditUser(new User());
			refreshUserList();
		}
		return null;
	}
	
	protected User getUserById(String id){
		return userService.findById(id);
	}


	public UserForm getUserForm() {
		return userForm;
	}


	public void setUserForm(UserForm userForm) {
		this.userForm = userForm;
	}


	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	
}
